clear
global OUT "${results}"
local var var
local ltfpvar tfpvar
local ii=1

*Rounding for bins  
local ltfpvar tfpvar
tempfile temp1
foreach svar in capT tau{
	foreach spec in RF FS{
		quietly import delimited "${results}/temp/OY_`spec'_`svar'_`ltfpvar'_${date}.csv", clear
		format * %9.4g
			if("`svar'"!="capT" | "`spec'"!="RF"){
				quietly merge 1:1 _n using `temp1', nogen
			}
		quietly save `temp1', replace
	}
	quietly import delimited "${results}/temp/`svar'_`ltfpvar'", clear
	format * %9.4g
	quietly merge 1:1 _n using `temp1', nogen		
	quietly save `temp1', replace
}
		
foreach var of varlist _all{
	rename `var' `var'_num
	cap gen `var'=string(`var'_num,"%9.4g")
	drop `var'_num
}
export delimited  "${results}/Fig3_and5cd.csv", replace






foreach sample in young old{
	use "${results}/temp/results${date}.dta" if sample=="`sample'" , clear
	foreach svar in tau capT{
		local var_`svar'_`sample'=var_`svar'[1]
		local printvar_`svar'_`sample' = string(`var_`svar'_`sample'', "%05.3f")
		local perm_var_`svar'_mean_`sample'=perm_var_`svar'_mean[1]
		local printperm_var_`svar'_mean_`sample' = string(`perm_var_`svar'_mean_`sample'', "%05.3f")
	}
	foreach svar in tau capT{
		use "${results}/temp/results_cov_${date}.dta" if sample=="`sample'",clear
		local mean_cov_`svar'_`sample'=mean_cov_`svar'[1]
		local printmean_cov_`svar'_`sample'= string(`mean_cov_`svar'_`sample'', "%05.3f")
		local bRPC_`svar'_`sample' 	= sqrt(`mean_cov_`svar'_`sample''/`var_`svar'_`sample'')
	}
}


foreach svar in capT tau{
	local sample all

	*Old/Young
	graph use "${results}/temp/kden_temp_`svar'_`ltfpvar'_young${date}"
	clear
	serset use 
	ren _all (d_`svar'_`ltfpvar'_young x_`svar'_`ltfpvar'_young discard)
	drop discard

	tempfile temp1
	save `temp1'

	graph use "${results}/temp/kden_temp_`svar'_`ltfpvar'_old${date}"
	clear
	serset use 
	ren _all (d_`svar'_`ltfpvar'_old x_`svar'_`ltfpvar'_old discard)
	drop discard		

	merge 1:1 _n using `temp1'


	foreach sample in old young{	
		*Old mean for new distribution
		sum x_`svar'_`ltfpvar'_`sample'
		local mu_X = 0

		*New location for new distribution
		local aGBAdj 	= ( 1 - `bRPC_`svar'_`sample'')*`mu_X'

		*New distribution points
		gen x_GBAdj_`sample'	= x_`svar'_`ltfpvar'_`sample'*`bRPC_`svar'_`sample'' + `aGBAdj'
		gen d_GBAdj_`sample'  	= d_`svar'_`ltfpvar'_`sample'/(`bRPC_`svar'_`sample'')
	}
	tempfile grapholdyoung`svar'
	save `grapholdyoung`svar''
}

	
local svar capT
use `grapholdyoung`svar''	

sum d_`svar'_`ltfpvar'_young
local m1 = r(max)
sum d_GBAdj_young
local m2 = r(max)
sum d_`svar'_`ltfpvar'_old 
local m3 = r(max)
sum d_GBAdj_old
local m4 = r(max)
local mxx = floor(max(`m1',`m2',`m3',`m4'))
local pt1 1
local pt2 1
local pt3 1
local pt4 1

twoway ///
(line d_`svar'_`ltfpvar'_young x_`svar'_`ltfpvar'_young, 	lcolor(black) 	lpattern(solid) sort lwidth(medthick)) 		///
(line d_GBAdj_young x_GBAdj_young, 	lcolor(blue) 	lpattern(longdash) sort lwidth(medthick)) 		///
(line d_`svar'_`ltfpvar'_old x_`svar'_`ltfpvar'_old, 		lcolor(black)  	lpattern(solid) sort lwidth(vthin) )  	///
(line d_GBAdj_old x_GBAdj_old, 	lcolor(blue) 	lpattern(longdash) sort lwidth(vthin)), 		///
graphregion(color(white)) bgcolor(white) plotregion(style(none)) ///
ytitle("Density") xtitle("Location Effect") ///
xlabel(-.4(.2).4,nogrid ) ylabel(0(2)6,nogrid ) ///
xscale(range([withheld] [withheld])) yscale(range(0 6.5)) ///
legend( lab(1 "                                       " " ") label(2 " " " ") label(3 " " " ") label(4 " " " ") position(2) ring(0) cols(1) region(lwidth(none) lcolor(none) color(none)) symxsize(*.5) )  ///
text(`pt1' [withheld]  "New Plants:                              " "Raw Var(`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:Y}) = `printvar_`svar'_young'", justification(left)) ///
text(`pt2' [withheld]  "New Plants: Bias-Corrected" "Cov(`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:YA},`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:YB}) = Var(`=ustrunescape("\u03BE")'{sub:l}{sup:Y}) = `printmean_cov_`svar'_young'", color(blue) justification(left)) ///
text(`pt3' [withheld] "Old Plants:                                " "Raw Var(`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:O}) = `printvar_`svar'_old'", color(black) justification(left)) ///
text(`pt4' [withheld]   "Old Plants: Bias-Corrected" "Cov(`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:OA},`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:OB}) = Var(`=ustrunescape("\u03BE")'{sub:l}{sup:O}) = `printmean_cov_`svar'_old'", color(blue) justification(left)) 
graph export "$OUT/Fig5a.png", replace



local svar tau
use `grapholdyoung`svar''

sum d_`svar'_`ltfpvar'_young
local m1 = r(max)
sum d_GBAdj_young
local m2 = r(max)
sum d_`svar'_`ltfpvar'_old 
local m3 = r(max)
sum d_GBAdj_old
local m4 = r(max)
local mxx = floor(max(`m1',`m2',`m3',`m4'))

local pt1 = `pt1'+[withheld]
local pt2 = `pt2'+[withheld]
local pt3 = `pt3'+[withheld]
local pt4 = `pt4'+[withheld]


twoway ///
(line d_`svar'_`ltfpvar'_young x_`svar'_`ltfpvar'_young, 	lcolor(black) 	lpattern(solid) sort lwidth(medthick)) 		///
(line d_GBAdj_young x_GBAdj_young, 	lcolor(blue) 	lpattern(longdash) sort lwidth(medthick)) 		///
(line d_`svar'_`ltfpvar'_old x_`svar'_`ltfpvar'_old, 		lcolor(black)  	lpattern(solid) sort lwidth(vthin) )  	///
(line d_GBAdj_old x_GBAdj_old, 	lcolor(blue) 	lpattern(longdash) sort lwidth(vthin)), 		///
graphregion(color(white)) bgcolor(white) plotregion(style(none)) ///
ytitle("Density") xtitle("Industry-Specific Location Effect") ///
xlabel(-.4(.2).4,nogrid ) ylabel(0(2)6,nogrid ) ///
xscale(range([withheld] [withheld])) yscale(range(0 6.5)) ///
legend( lab(1 "                                           " ) label(2 " " " " " ") label(3 " " "" ) label(4 " " " ") position(2) ring(0) cols(1) region(lwidth(none) lcolor(none) color(none)) symxsize(*.5) )  ///
text(`pt1' [withheld]  "New Plants:                                   " "Raw Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:Y}) = `printvar_`svar'_young'", justification(left)) ///
text(`pt2' [withheld]  "New Plants: Bias-Corrected          " "Cov(`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:YA},`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:YB}) = Var(`=ustrunescape("\u03C4")'{sub:l,i}{sup:Y}) = `printmean_cov_`svar'_young'", color(blue) justification(left)) ///
text(`pt3' [withheld] "Old Plants: Raw Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:O}) = `printvar_`svar'_old'  ", color(black) justification(left)) ///
text(`pt4' [withheld]   "Old Plants: Bias-Corrected            " "Cov(`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:OA},`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:OB}) =Var(`=ustrunescape("\u03C4")'{sub:l,i}{sup:O}) =`printmean_cov_`svar'_old'", color(blue) justification(left)) 
graph export "$OUT/Fig5b.png", replace
